package com.engine.cese.duss.srm.openbidhall.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.cese.duss.srm.util.CalUtil;
import com.engine.cese.duss.srm.util.GenUtil;
import com.weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-09
 * @Description: 开标大厅
 * @ModifyLog:
 **/

public class WeaOpenBidHallAction {

    /**
     * @Description: 根据采购立项单获取立项名称、
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getcglxd2Lxmc")
    @Produces({MediaType.TEXT_PLAIN})
    public String getcglxd2Lxmc(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JSONObject datas = new JSONObject();
            BaseBean baseBean = new BaseBean();
            String zbcglx = baseBean.getPropValue( "SrmWfTab", "zbcglx" );
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            RecordSet rs = new RecordSet();
            String sql = "select cglxmc from " + zbcglx + " where requestid=?";
            rs.executeQuery( sql, cglxd );
            rs.next();
            String cglxmc = Util.null2String( rs.getString( "cglxmc" ) );
            datas.put( "cglxmc", cglxmc );
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }
    /**
     * @Description: 根据loginID获取供应商名称
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getLoginId2Name")
    @Produces({MediaType.TEXT_PLAIN})
    public String getLoginId2Name(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JSONObject datas = new JSONObject();
            String userid = Util.null2String( request.getParameter( "userid" ) );
            int logintype = Util.getIntValue( request.getParameter( "logintype" ) );
            String name = "";
            if (logintype == 1) {
                name = GenUtil.getOaLoginId2Name( userid );
            } else {
                name = GenUtil.getCrmLoginId2Name( userid );
            }
            datas.put( "name", name );
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }
    /**
     * @Description: 根据已在线的人员的更新人员在线表中
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getUpdateRyzx")
    @Produces({MediaType.TEXT_PLAIN})
    public String getUpdateRyzx(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JSONObject datas = new JSONObject();
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            String userid = Util.null2String( request.getParameter( "userid" ) );
            int zxlx = Util.getIntValue( request.getParameter( "zxlx" ) );
            String sfzx = zxlx > 0 ? "离线" : "在线";
            String curdate = DateUtil.getCurrentDate();
            RecordSet rs = new RecordSet();
            String sql = "update uf_ryzxb set sfzx=?,zxlx=? where cglxd=? and ryid=? and zxrq=? ";
            Object[] params = {sfzx, zxlx, cglxd, userid, curdate};
            rs.executeUpdate( sql, params );
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 查询人员在线状态
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getQuqeyRyzx")
    @Produces({MediaType.TEXT_PLAIN})
    public String getQuqeyRyzx(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JSONObject datas = new JSONObject();
            JSONArray dataList = new JSONArray();
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            String curdate = DateUtil.getCurrentDate();
            RecordSet rs = new RecordSet();
            String sql = "select rymc,sfzx,zxlx from uf_ryzxb where cglxd=? and zxrq=?";
            rs.executeQuery( sql, cglxd, curdate );
            int counts = 0;
            while (rs.next()) {
                JSONObject dataJson = new JSONObject();
                String rymc = Util.null2String( rs.getString( "rymc" ) );
                String sfzx = Util.null2String( rs.getString( "sfzx" ) );
                int zxlx = Util.getIntValue( rs.getString( "zxlx" ) );
                if (zxlx == 0) {
                    counts++;
                }
                dataJson.put( "rymc", rymc );
                dataJson.put( "sfzx", sfzx );
                dataList.add( dataJson );
            }
            datas.put( "dataList", dataList );
            datas.put( "counts", counts );
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 查询供应商投标数据
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getGystbRepData")
    @Produces({MediaType.TEXT_PLAIN})
    public String getGystbRepData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JSONObject datas = new JSONObject();
            BaseBean baseBean = new BaseBean();
            String gystb = baseBean.getPropValue( "SrmWfTab", "gystb" );
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            String lc = Util.null2String( request.getParameter( "lc" ) );
            List<Map<String, String>> dataList = new ArrayList<>();
            RecordSet rs = new RecordSet();
            String sql = "select gysmc,tbwsze,sl,dense_rank() over( order by tbwsze asc) pm " +
                    "from " + gystb + " where cglxd=? and lc=?";
            rs.executeQuery( sql, cglxd, lc );
            while (rs.next()) {
                Map<String, String> dataMap = new HashMap<>();
                String gysmcid = Util.null2String( rs.getString( "gysmc" ) );
                String gysmc = GenUtil.getId2Gysmc( gysmcid );
                String tbwsze = Util.null2String( rs.getString( "tbwsze" ) );
                String sl = Util.null2String( rs.getString( "sl" ) );
                String slvalue = GenUtil.getId2Sl( sl );
                String pm = Util.null2String( rs.getString( "pm" ) );
                dataMap.put( "gysmc", gysmc );
                dataMap.put( "tbwsze", tbwsze );
                dataMap.put( "sl", slvalue );
                dataMap.put( "pm", pm );
                dataList.add( dataMap );
            }
            datas.put( "dataList", dataList );
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 根据采购立项单获取设置参数表中的开标时间
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getParSetBidsTime")
    @Produces({MediaType.TEXT_PLAIN})
    public String getParSetBidsTime(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JSONObject datas = new JSONObject();
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            String lc = Util.null2String( request.getParameter( "lc" ) );
            RecordSet rs = new RecordSet();
            BaseBean baseBean = new BaseBean();
            String kbsz = baseBean.getPropValue( "SrmWfTab", "kbsz" );
            String sql = "select kbsj from (\n" +
                    "select concat(t1.kbsj,':00')as kbsj from " + kbsz + " t1 \n" +
                    "left join workflow_requestbase t2 on t1.requestid=t2.requestid where " +
                    "t1.cglxd=? and t1.lc=? and t2.currentnodetype=3 order by t1.id desc) where rownum<2\n";
            rs.executeQuery( sql, cglxd, lc );
            rs.next();
            String kbsj = Util.null2String( rs.getString( "kbsj" ) );
            datas.put( "kbsj", kbsj );
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 根据采购立项单获取设置开标会签表中数据
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getUpdateSzkbhqTab")
    @Produces({MediaType.TEXT_PLAIN})
    public String getUpdateSzkbhqTab(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JSONObject datas = new JSONObject();
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            String szanParam = Util.null2String( request.getParameter( "szan" ) );
            String kbanParam = Util.null2String( request.getParameter( "kban" ) );
            String hqanParam = Util.null2String( request.getParameter( "hqan" ) );
            String curdate = DateUtil.getCurrentDate();
            RecordSet rs = new RecordSet();
            Object[] params = {szanParam, kbanParam, hqanParam, cglxd};
            String sql = "update uf_szkbhq set szan=?,kban=?,hqan=?,isfinish=1 where cglxd=?";
            boolean flag = rs.executeUpdate( sql, params );
            if (flag) {
                rs.executeQuery( "select szan,kban,hqan,nvl(isfinish,0) as isfinish from uf_szkbhq where cglxd=? and dqrq=?", cglxd, curdate );
                rs.next();
                String szan = Util.null2String( rs.getString( "szan" ) );
                String kban = Util.null2String( rs.getString( "kban" ) );
                String hqan = Util.null2String( rs.getString( "hqan" ) );
                String isfinish = Util.null2String( rs.getString( "isfinish" ) );
                datas.put( "szan", szan );
                datas.put( "kban", kban );
                datas.put( "hqan", hqan );
                datas.put( "isfinish", isfinish );
            }
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 根据采购立项单获取设置开标会签表中数据
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getQuerySzkbhqData")
    @Produces({MediaType.TEXT_PLAIN})
    public String getQuerySzkbhqData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JSONObject datas = new JSONObject();
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            String curdate = DateUtil.getCurrentDate();
            RecordSet rs = new RecordSet();
            rs.executeQuery( "select szan,kban,hqan,cgy,nvl(isfinish,0) as isfinish from uf_szkbhq where cglxd=? and dqrq=?", cglxd, curdate );
            rs.next();
            String szan = Util.null2String( rs.getString( "szan" ) );
            String kban = Util.null2String( rs.getString( "kban" ) );
            String hqan = Util.null2String( rs.getString( "hqan" ) );
            String cgy = Util.null2String( rs.getString( "cgy" ) );
            String isfinish = Util.null2String( rs.getString( "isfinish" ) );
            datas.put( "szan", szan );
            datas.put( "kban", kban );
            datas.put( "hqan", hqan );
            datas.put( "cgy", cgy );
            datas.put( "isfinish", isfinish );
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 根据采购立项单获取更新会签按钮状态
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getQueryHqBtn")
    @Produces({MediaType.TEXT_PLAIN})
    public String getQueryHqBtn(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            JSONObject jsonData = new JSONObject();
            RecordSet rs = new RecordSet();
            BaseBean baseBean = new BaseBean();
            String kbjghq = baseBean.getPropValue( "SrmWfTab", "kbjghq" );
            String sql = "select t2.currentnodetype from " + kbjghq
                    + " t1 left join workflow_requestbase t2 on t1.requestid=t2.requestid where and t1.cglxd = ?";
            rs.executeQuery( sql,cglxd );
            rs.next();
            String currentnodetype = Util.null2String( rs.getString( "currentnodetype" ) );
            jsonData.put( "currentnodetype", currentnodetype );
            apidatas.put( "datas", jsonData );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

}
